Page({
    data: {
        xPageReady : true ,
        xContainer : false ,
        xWarning : false ,
        xSaleMoney : 29 ,
        xValue: '' ,
        xProduct: '' ,
        statusBar:'',
        doctor:''
    },

    onLoad: function (options) {
        wx.getSystemInfo({
          success: (e) => {
            let custom = wx.getMenuButtonBoundingClientRect();
            this.setData({
              statusBar : e.statusBarHeight
            })
          }
        })
        this.xInit();
        this.user();
    },

    xInit: async function () {
        let that = this

        await wx.cloud.callFunction({
            name: 'product',
            data: {
                action: "get_letter_chat_product"
            }
        }).then(res => {
            console.log('写信产品' , res.result)
            that.setData({
                xProduct : res.result
            })
        })

        that.setData({
            xPageReady : false
        })
    },
    user : function () {
      let that = this
      wx.cloud.callFunction({
          name : 'user' ,
          data : {
              action : 'detail'
          }
      }).then( res => {
          let user = res.result
          console.log('用户' ,user)
          that.setData({
              doctor : user.consultation_teacher_info[0] ,
          })
      })
    },
    toback: function ( ) {
        wx.navigateBack({
          delta: 1,
        })
    },
    xWarningShow: function () {
        let that = this
        that.setData({
            xPaying : false,
            xWarning : true,
            xContainer : true
        })
    },

    xBuy: function () {
        let that = this
        let value = that.data.xValue
        if(value == ''){
            wx.showToast({
                title: '请输入后再发送',
                icon: 'none'
            })
        }else{
            that.setData({
                xPaying : true,
                xWarning : false,
                xContainer : true
            })
        }
    },

    xSubmit: function (e) {
        let that = this
        let money = that.data.xSaleMoney
        that.paymentNext(money)
    },

    xCheckInput: function (e) {
        let that = this
        let value = e.detail.value
        that.setData({
            xValue : value
        })
    },

    paymentNext: async function(price){
        let that = this
        wx.showLoading({
            title: '支付准备中',
        })
        let currentTime = new Date()
        let year = currentTime.getFullYear
        let m = currentTime.getMonth
        let day = currentTime.getDay
        let timestamp = ""+(new Date()).getTime()
        let product = that.data.xProduct
        that.prepay(product.name, price, timestamp, product._id)
    },

    prepay: function(productName, price, timestamp, productId){
        let that = this
        let product = that.data.xProduct
        wx.cloud.callFunction({
            name : 'payment',
            data: {
                action: 'prepay',
                productName: productName,//商品名称
                price: price, //价格that.data.seriesLessonsData.price
                timestamp: timestamp,
                productId: productId,
                planId: product.plan_id
            },
            success(res) {
                wx.hideLoading()
                let payData = res.result
                let timeStamp = payData.payment.timeStamp
                let totalFee = price
                that.pay(payData.payment, totalFee, timeStamp, productId)
            }
        })
    },


    pay: function(payData, totalFee, timeStamp, productId){
        let that = this
        let value = that.data.xValue
        let doctor = that.data.doctor
        wx.requestPayment({
            timeStamp: payData.timeStamp,
            nonceStr: payData.nonceStr,
            package: payData.package, //统一下单接口返回的 prepay_id 格式如：prepay_id=***
            signType: 'MD5',
            paySign: payData.paySign, //签名
            success(res) {
                // wx.showLoading({
                //     title: '发送中',
                // })
                let _dataObj ={
                    to_openid: doctor._openid ,
                    message_type: 1,
                    business_type: 7,
                    content: {
                        "title" : "心理聊愈",
                        "content" : {
                            "text" : that.data.xValue,
                        }
                    }
                }
                wx.cloud.callFunction({
                    name: 'consulting_message',
                    data: {
                        action: "send_message",
                        dataObj: _dataObj
                    }
                }).then(res => {
                    wx.showToast({
                        title: '支付成功',
                        icon: 'none'
                    })
                    wx.navigateTo({
                      url: '/subsale/pages/email/index/index',
                    })
                })
            },
            fail(res) {
                console.log("支付失败", res)
            },
            complete(res) {
                console.log("支付完成,complete res :", res)
            }
        })
    },
})